/*
 *  Copyright 2003 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *  
 */
#include <std.h>
#include <stdio.h>
#include <csl.h>
#include <csl_cache.h>
#include <csl_dat.h>
#include <chan.h>
#include <scom.h>
#include <utl.h>
#include "fvid.h"
#include "cellh263enc.h"  
#include "cellh263dec.h"  
#include "appmain.h"
#include "appThreads.h"
#include "tskProcess.h"

static unsigned char  y[FRAME_BUF_SIZE];
static unsigned char  cb[FRAME_BUF_SIZE/2];
static unsigned char  cr[FRAME_BUF_SIZE/2];
static unsigned char* yuvFrame[3] = { y, cb, cr};
static unsigned char  bitBuf[BIT_BUF_SIZE];
             

/* ======================================================================== */
/* H.263 encoder parameters for the test case                               */
/* ======================================================================== */
volatile IH263ENC_Params h263encParams = {
sizeof(IH263ENC_Params),
    4096,                   
    25,                     
    132,                     
    4,
    4,
    31,                                          
#ifdef _NTSC
    H263_SRCFMT_720X480,        
#elif defined _PAL
    H263_SRCFMT_720X576,   
#endif         
    15                       
};

static ICELL_Obj  loopbackChanCells[] = {
    {
        sizeof(ICELL_Obj),
    	NULL, 
    	&H263ENC_CELLFXNS,
    	NULL,
    	(IALG_Fxns *)&H263ENC_IH263ENC,
    	(IALG_Params *)&h263encParams,
    	NULL,
    	0, 
    	NULL,
    	1,
    	NULL,
    	1
    }, 
    {
        sizeof(ICELL_Obj),
    	NULL, 
    	&H263DEC_CELLFXNS,
    	NULL,
    	(IALG_Fxns *)&H263DEC_IH263DEC,
    	NULL,
    	NULL,
    	0, 
    	NULL,
    	1,
    	NULL,
    	1
    } 

};


/*Varibale to modify the bitrate on the fly*/
volatile Bool bitRateChanged = FALSE;
volatile int bitRateTarget;

ThrProcess thrProcess;

void tskProcessInit()
{
	int chanNum;
	Bool rc;
	ICELL_Obj  *cell;
	ICC_Handle  inputIcc;
    ICC_Handle  outputIcc;
    thrProcess.bufIntermediate = bitBuf; 
    for (chanNum = 0; chanNum < PROCESSNUMCHANNELS ; chanNum++) 
    {


    /*------------------------------------------------------------*/ 
    /*  register the cells: define what will be input buffers and */
    /*  what will be output buffers for each cell.                */
    /*------------------------------------------------------------*/
                
        /*-----------------------------------------------------------*/
        /* Setup a default cell used to initialize the actual cells  */
        /*-----------------------------------------------------------*/
        ICELL_Obj    defaultCell = ICELL_DEFAULT;

        /*-----------------------------------------------------------*/
        /* Register the cell for H263 encoder                        */
        /*-----------------------------------------------------------*/
        cell = &thrProcess.cellList[ (chanNum * PROCESSNUMCHANNELS ) + CELLH263ENC];
        *cell                = defaultCell;
        cell->name           = "H263ENC";
        cell->cellFxns       = &H263ENC_CELLFXNS;            
        cell->algFxns        = (IALG_Fxns *)&H263ENC_IH263ENC;
        cell->algParams      = (IALG_Params *)&h263encParams;
        cell->scrBucketIndex = THRPROCESSSCRBUCKET;

        /*-----------------------------------------------------------*/
        /* Create the inputICC: object used as input interface to the*/ 
        /* enocoder cell                                                  */
        /*-----------------------------------------------------------*/
        inputIcc  = (ICC_Handle)ICC_linearCreate( 
                               thrProcess.bufInput[ chanNum ], 
                               sizeof(unsigned char*)*3);
        UTL_assert( inputIcc != NULL);

		/*-----------------------------------------------------------*/
        /* Create the outputICC: object used as output interface to  */ 
        /* enocoder cell                                             */
        /*-----------------------------------------------------------*/


        outputIcc = (ICC_Handle)ICC_linearCreate( 
                                            thrProcess.bufIntermediate,
                                            sizeof(BIT_BUF_SIZE));
        UTL_assert( outputIcc != NULL);

        /*------------------------------------------------------------*/
        /* Only one input and one output ICC are needed.              */
        /*------------------------------------------------------------*/
        rc = CHAN_regCell( cell, &inputIcc, 1, &outputIcc, 1 );
 
        
        /*------------------------------------------------------------*/
        /* Register the decoder cell                                  */
        /*------------------------------------------------------------*/
        cell = &thrProcess.cellList[ (chanNum * PROCESSNUMCHANNELS ) + CELLH263DEC];
        *cell                = defaultCell;
        cell->name           = "H263DEC";
        cell->cellFxns       = &H263DEC_CELLFXNS;            
        cell->algFxns        = (IALG_Fxns *)&H263DEC_IH263DEC;
        cell->algParams      = NULL;
        cell->scrBucketIndex = THRPROCESSSCRBUCKET;

   		/*-----------------------------------------------------------*/
        /* The input ICC for the decoder will be same as outputICC of*/
        /* the encoder. This is used to pass the generated bitstream */
        /* from encoder cell to decoder cell                         */ 
        /*-----------------------------------------------------------*/

        inputIcc  = outputIcc;
        UTL_assert( inputIcc != NULL);


		/*-----------------------------------------------------------*/
        /* Create the outputICC: object used as output interface to  */ 
        /* enocoder cell                                             */
        /*-----------------------------------------------------------*/
         outputIcc = (ICC_Handle)ICC_linearCreate( 
                                            thrProcess.bufOutput[chanNum],
                                            sizeof(unsigned char*)*3);
         UTL_assert( outputIcc != NULL);

        /*------------------------------------------------------------*/
        /* Only one input and one output ICC are needed.              */
        /*------------------------------------------------------------*/
        rc = CHAN_regCell( cell, &inputIcc, 1, &outputIcc, 1 );
        
        thrProcess.cellList[ (chanNum * PROCESSNUMCELLS) + CELLH263ENC].algParams = 
            (IALG_Params *)&h263encParams;
    
        UTL_logDebug1("Channel Number: %d", chanNum);
        UTL_assert( rc == TRUE );

    }

                  
}                   


/*-------------------------------------------------------*/
/* Create the channel instance : the cell algorithms will*/
/* be instantiated                                       */
/*-------------------------------------------------------*/
void tskProcessStart() 
{
		int chanNum;
		Bool rc;
		for (chanNum = 0; chanNum < PROCESSNUMCHANNELS ; chanNum++) 
		{
		
			/*------------------------------------------------------------*/
    		/* Open the channel: this causes the algorithms to be created */
            /*------------------------------------------------------------*/
			rc = CHAN_open( &thrProcess.chanList[ chanNum ], 
			                &thrProcess.cellList[ chanNum * PROCESSNUMCELLS ], 
			                PROCESSNUMCELLS ,
			                NULL );
		
		}
		UTL_assert( rc == TRUE );
}


/*-------------------------------------------------------*/
/* The task will handle the processing part :            */
/* -Will get the message from the Input task with input  */
/*  frame pointers                                       */
/* -Will execute the channel to encode followed by decode*/
/* -Will pass the decoded frame pointers to output task  */
/*-------------------------------------------------------*/

void tskProcess()
{
    int i;
    int chanNum;
    Bool rc;
    ScomBufChannels *pMsgBuf;  
    unsigned char *frame;          

    
    SCOM_Handle fromInputtoProc,fromProctoInput,fromProctoOut;
    fromInputtoProc = SCOM_open("INTOPROC");
    fromProctoInput = SCOM_open("PROCTOIN");
    fromProctoOut   = SCOM_open("PROCTOOUT");
    while(1)
    {
        for(i = 0; i<PROCESSNUMCHANNELS; i++) 
        {
        
 	     	/*-----------------------------------------------------------*/
	  		/* Wait for the message from  input task to recieve captured */
  			/* frame to be cycled through encoding and decoding.         */
   			/*-----------------------------------------------------------*/
	 	    pMsgBuf = SCOM_getMsg(fromInputtoProc, SYS_FOREVER);
	 	    frame = (unsigned char *)pMsgBuf->bufChannel;
	 	     
			for( chanNum = 0; chanNum < PROCESSNUMCHANNELS; chanNum++ ) 
	 	    {
	
				CHAN_Handle chanHandle = &thrProcess.chanList[ chanNum ];
	
	            /*------------------------------------------------------*/
	            /* Check if the change in the bitrate has been requested*/
	            /* Copy desired bitrate in the encoder params structure */
	            /* Call contol function to set the modified  params     */
	            /*------------------------------------------------------*/
	            if(bitRateChanged) 
	           	{
	               h263encParams.bitRate = bitRateTarget;
	 	     	   H263ENC_cellControl(&(chanHandle->cellSet[CELLH263ENC]),IH263ENC_SETPARAMS, (void *)&h263encParams);
	 	     	   bitRateChanged = FALSE;
	 	     	}
		            
	            
	            /*----------------------------------------------------------*/
	            /* Set the input ICC buffer for H263ENC cell for each channel*/
	            /*----------------------------------------------------------*/
	            ICC_setBuf(chanHandle->cellSet[CELLH263ENC].inputIcc[0],
	                       frame, 
	                       sizeof(unsigned char*)*3);
	
	            /*-------------------------------------------------------*/
	            // Set the output ICC buffer for H263DEC cell for each channel
	            /*-------------------------------------------------------*/
	            ICC_setBuf(chanHandle->cellSet[CELLH263DEC].outputIcc[0],
	                       yuvFrame,
	                       sizeof(unsigned char*)*3);
	
	            /*-------------------------------------------------------*/
	            // start the stopwatch
	            /*-------------------------------------------------------*/
	            UTL_stsStart( stsTime1 );  
	            
	            /*-------------------------------------------------------*/
	            // execute the channel
	            /*-------------------------------------------------------*/
	            rc = CHAN_execute( chanHandle, NULL );
	            UTL_assert( rc == TRUE );
	            
	            /*-------------------------------------------------------*/
	            // elapsed time goes to this STS
	            /*-------------------------------------------------------*/
	            UTL_stsStop( stsTime1 );    
	        }
	   		
	   		/*----------------------------------------------------------------*/
	   		/* Send the message to the input task to continue with next frame */
	   		/*----------------------------------------------------------------*/
	   		SCOM_putMsg(fromProctoInput,&(thrProcess.scomMsgRx));  	   	
	   		
	   		/*-----------------------------------------------------------*/
	   		/* Send message to output task with pointers to decoded frame*/
	   		/*-----------------------------------------------------------*/
	   		thrProcess.scomMsgTx.bufChannel=yuvFrame;
	   		SCOM_putMsg(fromProctoOut,&(thrProcess.scomMsgTx));

 		}
 	}	                                                 
}
